// import Axios from 'axios'
const Axios = require("axios");
const qs = require("qs");

const { FACE_KEY, FACE_SECRET } = process.env;

// https://console.faceplusplus.com.cn/dashboard
const axios = Axios.create({
  baseURL: "https://api-cn.faceplusplus.com/facepp/v3",
});

// https://console.faceplusplus.com.cn/documents/4887586
export async function compareFace(image_url1, image_url2) {
  if (!image_url1 || !image_url2) {
    throw "缺少参数";
  }
  try {
    const { data } = await axios.post(
      "/compare",
      qs.stringify({
        api_key: FACE_KEY,
        api_secret: FACE_SECRET,
        image_url1,
        image_url2,
      })
    );
    return data;
  } catch (error) {
    let data = error.response?.data;
    if (data) {
      throw {
        msg: data.error_message,
        time_used: data.time_used,
        status: error.response.status,
      };
    }
    throw error.message;
  }
}

/*
compareFace(
  "https://qs-1252071497.cos.ap-beijing.myqcloud.com/app/22/user/3621721199207_.pic_thumb.jpg",
  "https://qs-1252071497.cos.ap-beijing.myqcloud.com/app/22/user/3641721199232_.pic_thumb.jpg"
)
  .then(console.log)
  .catch(console.log);

返回
{
  request_id: '1721199352,0cbe6d27-be00-4094-9461-77f83eadbddc',
  time_used: 670,
  confidence: 94.749,
  thresholds: { '1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975 },
  faces1: [
    {
      face_token: '5d4ffe86552df6a87f25c1b127267b21',
      face_rectangle: [Object]
    }
  ],
  faces2: [
    {
      face_token: '1dd0a498243346cb8a99adf49bb22710',
      face_rectangle: [Object]
    }
  ],
  image_id1: 't5HtAd8ZVnegfuiRkmw57g==',
  image_id2: 'Are8s8KW7j1Qz5oVjmrP/w=='
}
*/
